package com.zhongshengnetwork.rightbe.wzt.view;

import android.graphics.Canvas;
import android.graphics.Color;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import static androidx.recyclerview.widget.ItemTouchHelper.ACTION_STATE_DRAG;

public class ItemDragCallback extends ItemTouchHelper.Callback {
    private WZTOrderRecyclerAdapter mAdapter;

    public ItemDragCallback(WZTOrderRecyclerAdapter mAdapter) {
        this.mAdapter = mAdapter;
    }


    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        WZTOrderRecyclerAdapter adapter = (WZTOrderRecyclerAdapter) recyclerView.getAdapter();
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getAdapterPosition();   //拖动的position
        int toPosition = target.getAdapterPosition();     //释放的position
        mAdapter.itemMove(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }


    @Override
    public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        if (dX != 0 && dY != 0 || isCurrentlyActive) {
            WZTOrderRecyclerAdapter adapter = (WZTOrderRecyclerAdapter) recyclerView.getAdapter();
            //mEdit = adapter.isEdit;
        }
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if (actionState == ACTION_STATE_DRAG) {
            //长按时调用
            WZTOrderRecyclerAdapter.ViewHolder holder = (WZTOrderRecyclerAdapter.ViewHolder) viewHolder;
            holder.wzt_content_layout.setBackgroundColor(Color.parseColor("#CCCCCC"));
        }
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        //松手时调用
        WZTOrderRecyclerAdapter.ViewHolder holder = (WZTOrderRecyclerAdapter.ViewHolder) viewHolder;
        holder.wzt_content_layout.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }
}
